package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.ProductSkuProperty;
import cn.wolfcode.shop.mapper.ProductSkuMapper;
import cn.wolfcode.shop.mapper.ProductSkuPropertyMapper;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wolfcode on 2018/8/20.
 */
@Service@Transactional
public class ProductSkuServiceImpl implements IProductSkuService {
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductSkuPropertyMapper productSkuPropertyMapper;
    public void saveSku(GenerateSkuVo generateSkuVo) {
        for(ProductSku productSku:generateSkuVo.getProductSkuList()){
            productSku.setProductId(generateSkuVo.getProduct().getId());
            productSkuMapper.insert(productSku);
            for(ProductSkuProperty productSkuProperty:productSku.getProductSkuPropertyList()){
                productSkuProperty.setProductSkuId(productSku.getId());
                productSkuPropertyMapper.insert(productSkuProperty);
            }
        }
    }

    public List<ProductSku> queryByProductId(Long productId) {
        return productSkuMapper.queryByProductId(productId);
    }

    public ProductSku get(Long productSkuId) {
        return productSkuMapper.selectByPrimaryKey(productSkuId);
    }
}
